package stage_1_module_3.code.homework_5;

import java.util.Comparator;

public class Card implements Comparable<Card>, Comparator<Card> {
    private int weight;
    private String code;
    //1-红桃;2-方块;3-梅花;4-黑桃
    private int color;
    public Card(String code) {
        setCard(code, 1);
    }
    public Card(String code, int color) {
        setCard(code, color);
    }

    private void setCard(String code, int color) {
        this.color = color;
        this.code = code;
        switch (code) {
            case "大王":
                this.weight = -150 + color;
                break;
            case "小王":
                this.weight = -140 + color;
                break;
            case "2":
                this.weight = -130 + color;
                break;
            case "A":
                this.weight = -120 + color;
                break;
            case "K":
                this.weight = -110 + color;
                break;
            case "Q":
                this.weight = -100 + color;
                break;
            case "J":
                this.weight = -90 + color;
                break;
            case "10":
                this.weight = -80 + color;
                break;
            case "9":
                this.weight = -70 + color;
                break;
            case "8":
                this.weight = -60 + color;
                break;            
            case "7":
                this.weight = -50 + color;
                break;            
            case "6":
                this.weight = -40 + color;
                break;
            case "5":
                this.weight = -30 + color;
                break;
            case "4":
                this.weight = -20 + color;
                break;
            case "3":
                this.weight = -10 + color;
                break;
            default:
                break;
        }
    }

    public int getWeight() {
        return this.weight;
    }

    @Override
    public int compareTo(Card c) {
        return this.getWeight() - c.getWeight();
    }

    @Override
    public String toString() {
        String prefix = "";
        if (!this.code.equals("大王") && !this.code.equals("小王")) {
            switch (this.color) {
                case 1:
                    prefix = "♥";
                    break;
                case 2:
                    prefix = "♦";
                    break;
                case 3:
                    prefix = "♣";
                    break;
                case 4:
                    prefix = "♠";
                    break;
                default:
                    break;
            }
        }
        return prefix+this.code;
    }

    @Override
    public int compare(Card o1, Card o2) {
        return o1.getWeight() - o2.getWeight();
    }
}